package org.blogwu.learn.arithmetic.demo1;

import java.util.Arrays;

public class Test1 {

    public static void main(String[] args) {

        {
            int[] sz = {};
            System.out.println(Arrays.toString(plusOne(sz)));//{}
        }
        {
            int[] sz = {1, 2, 3};
            System.out.println(Arrays.toString(plusOne(sz)));//{1,2,4}
        }
        {
            int[] sz = {4, 3, 2, 1};
            System.out.println(Arrays.toString(plusOne(sz)));//{4,3,2,2}
        }
        {
            int[] sz = {1, 9};
            System.out.println(Arrays.toString(plusOne(sz)));//{2,0}
        }
        {
            int[] sz = {9, 9};
            System.out.println(Arrays.toString(plusOne(sz)));//{1,0,0}
        }


    }

    public static int[] plusOne(int[] digits) {
        if (digits.length <= 0) return digits;
        int[] newSz;
        boolean check = false;
        // 如果第一位为9.那么极有可能超过
        newSz = new int[digits.length + 1];

        for (int i = digits.length - 1; i >= 0; i--) {
            int last = digits[i];
            int indexValue = last + 1;
            if (indexValue > 9) {
                digits[i] = 0;
                newSz[i + 1] = 0;
                continue;
            }
            digits[i] = indexValue;
            return digits;
        }
        newSz[0] = 1;
        return newSz;
    }
}
